<template>
	<view class="container">
		<loading :show="loading_show"></loading>
		<customHead
			title="使用优惠券"
			titlePaddingLeft="30"
			:isShowBackArrow="true"
			navi_background="#fff"
		>
			<!-- <template v-slot:title>
				<view class="title_box_slot">
					<view class="leftIcon" @click="goback">
						<u-icon name="arrow-left" size="20"></u-icon>
					</view>
					<view class="search_box">
						<input class="input" type="text" placeholder="搜索优惠券适用商品"/>
						<u-icon name="search" size="20" color="#01cb88"></u-icon>
					</view>
				</view>
			</template> -->
			<template>
				<view class="bottom_text">
					<text v-if="couponDetail.couponType == 0">满￥{{couponDetail.howMuchFullMoney}}减￥{{couponDetail.couponDenomination}}</text>
					<text v-if="couponDetail.couponType == 3">满￥{{couponDetail.howMuchFullMoney}}打{{couponDetail.couponDenomination * 10}}折</text>
				</view>
			</template>
		</customHead>
		<view class="goods_main">
			<scroll-view 
				scroll-y="true" 
				class="scroll"
				@scrolltolower="scrolltolower"
				refresher-enabled 
				:show-scrollbar="true"
				@refresherrefresh="refresherrefresh"
				:refresher-triggered="refresher_show"
			>
				<refresh-view :pullDis="pullDis" :height="naviHeight"></refresh-view>
				<view class="goodsList_box">
					<view class="goods_item _clickActive" @click="gotoGoodsDetail(item.id)" v-for="(item, index) in goodsList" :key="index">
						<view class="goodsTitle_box">
							<view class="image">
								<image :src="item.rotationImgsJson[0]" lazy-load mode="aspectFill"></image>
							</view>
							<view class="title">
								<text class="title_text" :style="{color: _temp_titleColor}">{{item.goodsName}}</text>
								<text class="desc_text" :style="{color: _temp_textColor}">{{item.goodsRemarks}}</text>
								<view class="couponAfter">
									<text>优惠后￥{{disCount(item.goodsPrice)}}</text>
								</view>
								<view class="hotSale">
									<text>热销</text>
								</view>
								<!-- <view class="coupondesc">
									<view class="coupon">
										<view class="text1">券</view>
										<view class="text2">满600减10</view>
									</view>
								</view> -->
								<view class="price">
									<view class="">
										<span>￥</span>
										<text>
											{{item.goodsPrice}}
										</text>
									</view>
									<image class="image" @click.native.stop="addCart(item)" src="@/static/images/home/cart.png" mode="aspectFill"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- <view class="bottom_submit">
				<view class="left">
					<view class="count">
						<text>小计：</text>
						<text class="number">￥0.00</text>
					</view>
					<view class="desc">
						<text>再买</text>
						<text class="num">￥100.00</text>
						<text>可用优惠券</text>
					</view>
				</view>
				<view class="right">
					<button class="button _btn_base" @click="gotoCart" :style="{backgroundColor:item.color}">去购物车</button>
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		searchPlugsGoodCategoryList,
		searchPlugsGoodsByCategoryId,
		searchPlugsCouponsById,
		searchPlugsGoodsList,
		searchPlugsCouponsByBusinessId
	} from '@/tools/request/api.js'
	
	export default {
		data() {
			return {
				slideHeight: 84,
				goosList: [],
				couponList: [],
				goodsPage: 1,
				totalPage: 0,
				refresher_show: false,
				goodsList: [],
				slideHeight: 70,
				couponId: 0,
				couponDetail: {},
				loading_show: false
			};
		},
		computed: {
			// 计算优惠后的价格
			disCount () {
				return function (goodsPrice) {
					if (goodsPrice >= this.couponDetail.howMuchFullMoney) {
						if (this.couponDetail.couponType  == 3) {
							return (goodsPrice * this.couponDetail.couponDenomination).toFixed(2);
						}
						if (this.couponDetail.couponType  == 0) {
							return (goodsPrice - this.couponDetail.couponDenomination);
						}
					} else {
						return goodsPrice;
					}
				}
			},
			// 匹配可使用的优惠券
			matchAvaliableCoupon () {
				
			}
		},
		onLoad (option) {
			this.couponId = option.couopnId;
			this.getcouponDetail(option.couopnId);
			this.getcouponList();
		},
		methods: {
			goback () {
				uni.navigateBack();
			},
			gotoCart () {
				uni.switchTab({
					url: '/pages/cart/cart'
				})
			},
			gotoGoodsDetail (id) {
				uni.navigateTo({
					url: `/subpack_category/goodsDetail/goodsDetail?id=${id}`
				})
			},
			addCart (item) {
				this.addCartGoods(item);
			},
			// 获取优惠券详情
			getcouponDetail(id) {
				searchPlugsCouponsById({ id: id}).then((res) => {
					if ((res.code = 200)) {
						this.couponDetail = res.body;
						this.getGoodsList(this.couponDetail.goodsIds);
					} else {
						uni.$showMsg(res.msg);
					}
				});
			},
			// 通过优惠券绑定的商品ids来获取商品列表
			getGoodsLisByCoupon () {
				
			},
			// 获取所有的优惠券列表
			getcouponList(clear) {
				this.loading_show = true;
				let postdata = {
					businessId: this.businessId,
					page: 1,
					size: 100,
					userServiceToken: this.userServiceToken
				};
				searchPlugsCouponsByBusinessId(postdata).then((res) => {
					this.loading_show = false;
					if (res.code != 200) return uni.$showMsg();
					this.couponList = res.body.list;
				});
			},
			
			// 根据分类id获取商品列表
			getGoodsList (id) {
				id = id.toString();
				this.loading_show = true;
				let data = {
					page: this.goodsPage,
					size: 100,
					businessId: this.businessId,
					categoryId: '-1',
					ids: id
				};
				searchPlugsGoodsList(data).then(res => {
					this.loading_show = false;
					if (res.code == 200) {
						this.totalPage = res.body.pages;
						let list = res.body.list;
						list.forEach((item) => {
							if (item.rotationImgsJson) {
								item.rotationImgsJson = JSON.parse(item.rotationImgsJson);
							}
						})
						if (this.refresher_show) {
							this.goodsList = list;
						} else {
							this.goodsList = this.goodsList.concat(list);
						}
						this.refresher_show = false;
					} else {
						uni.$showMsg();
					}
					this.currentId = id;
				})
			},
			// 滚动到底部加载更多
			scrolltolower () {
				if (this.goodsPage >= this.totalPage) return
				this.goodsPage++;
				this.getGoodsList(this.currentId);
			},
			// 下拉刷新
			refresherrefresh () {
				this.refresher_show = true;
				this.goodsPage = 1;
				this.getGoodsList(this.currentId);
			},
		},
		
	}
</script>

<style lang="scss" scoped>
	.container{
		width: 100vw;
		height: 100vh;
		
		.title_box_slot{
			width: 100%;
			height: 100%;
			display: flex;
			align-items: center;
			position: relative;
			
			.leftIcon{
				width: 80rpx;
				height: 100%;
				display: flex;
				align-items: center;
			}
			
			.search_box{
				width: 320rpx;
				height: 64rpx;
				border-radius: 32rpx;
				padding: 0 20rpx;
				background-color: #e6e6e6;
				display: flex;
				align-items: center;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				
				.input{
					font-size: 28rpx;
				}
			}
		}
		
		.slide_box{
			width: 100%;
			padding: 0 20rpx;
			padding-top: 20rpx;
			box-sizing: border-box;
			
			.input_box{
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
				display: flex;
				justify-content: center; 
				align-items: center;
				background-color: #fff;
				
				.text{
					font-size: 26rpx;
					color: #9f9f9f;
				}
			}
		}
		
		.bottom_text{
			width: 100%;
			height: 70rpx;
			padding-bottom: 20rpx;
			padding-left: 30rpx;
			padding-right: 30rpx;
			box-sizing: border-box;
			
			text{
				display: block;
				width: 100%;
				height: 50rpx;
				line-height: 50rpx;
				border-radius: 25rpx;
				text-align: center;
				background-color: #FFDEE2;
				color: #FF1929;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
		.goods_main{
			
			.scroll{
				height: 100vh;
				
				.goodsList_box{
					box-sizing: border-box;
					border-left: 1rpx solid #ddd;
					background-color: #fff;
					border-bottom-left-radius: 20rpx;
					border-bottom-right-radius: 20rpx;
					margin-bottom: 30rpx;
					// animation-name: show;
					// animation-duration: 0.2s;
					// animation-fill-mode: forwards;
					
					.goods_item{
						border-bottom: 1rpx solid #e6e6e6;
						padding: 20rpx 20rpx;
						box-sizing: border-box;
						border-bottom: 1rpx solid #c8c8c8;
					
						.goodsTitle_box{
							display: flex;
							align-items: center;
							
							.image{
								width: 220rpx;
								height: 220rpx;
								border-radius: 20rpx;
								
								image{
									width: 100%;
									height: 100%;
									border-radius: 20rpx;
								}
							}
							
							
							.title{
								flex: 1;
								display: flex;
								flex-direction: column;
								margin-left: 20rpx;
								box-sizing: border-box;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
								
								.title_text{
									font-size: 30rpx;
									padding-bottom: 14rpx;
									overflow: hidden;
									white-space: normal;
									text-overflow: ellipsis;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
								}
								.desc_text{
									color: #7a7a7a;
									font-size: 24rpx;
									overflow: hidden;
									text-overflow: ellipsis;
								}
								.couponAfter{
									
									
									text{
										padding: 4rpx 16rpx;
										background-color: #FF0000;
										color: #fff;
										font-size: 26rpx;
										border-radius: 10rpx;
									}
								}
								.hotSale{
									padding: 4rpx 0;
									
									text{
										padding: 4rpx 4rpx;
										background-color: #FFDEE2;
										color: #FF0000;
										font-size: 22rpx;
										border-radius: 10rpx;
									}
								}
								.coupondesc{
									display: flex;
									align-items: center;
									margin-top: 6rpx;
									
									.coupon{
										border: 1rpx solid #FF0000;
										display: flex;
										align-items: center;
										border-radius: 6rpx;
										
										.text1{
											width: 26rpx;
											padding: 4rpx 4rpx;
											background-color: #FF0000;
											color: #fff;
											font-size: 22rpx;
											border-radius: 6rpx;
										}
										.text2{
											padding: 0 4rpx;
											font-size: 22rpx;
											color: #FF0000;
										}
									}
								}
								.price{
									margin-top: 20rpx;
									font-size: 34rpx;
									color: #FF0000;
									font-weight: bold;
									display: flex;
									justify-content: space-between;
									align-items: center;
									
									span{
										font-size: 30rpx;
									}
									.image{
										width: 50rpx;
										height: 50rpx;
									}
								}
								
							}
						}
						
					}
					.goods_item:last-child{
						border: none;
					}
				}
			}
			.bottom_submit{
				width: 100%;
				position: fixed;
				bottom: 0;
				left: 0;
				padding: 30rpx 30rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #fff;
				box-shadow: 0 -5rpx 10rpx 5rpx #f0f0f0;
				
				.left{
					color: #505050;
					.number{
						font-size: 28rpx;
						color: #FF0000;
						font-weight: bold;
					}
					.desc{
						color: #505050;
						.num{
							font-weight: bold;
						}
					}
				}
				.right{
					display: flex;
					align-items: center;
					
					.button{
						width: 160rpx;
						height: 65rpx;
						border-radius: 35rpx;
						font-size: 28rpx;
						color: #fff;
						padding: 0;
					}
				}
			}
		}
	}
</style>
